package com.jztd.servone.invoice.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.xbatis.core.sql.executor.chain.QueryChain;
import cn.xbatis.datasource.routing.DS;
import com.jztd.servone.invoice.mapper.BusinessConfigMapper;
import com.jztd.servone.invoice.mapper.InvoicePartyConfigMapper;
import com.jztd.servone.invoice.po.BusinessConfig;
import com.jztd.servone.invoice.po.InvoicePartyConfig;
import com.jztd.servone.invoice.service.InvoicePartyConfigService;
import com.jztd.platfrom.base.exception.PermissionDeniedException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@DS("invoice")
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class InvoicePartyConfigServiceImpl implements InvoicePartyConfigService {
    private final InvoicePartyConfigMapper invoicePartyConfigMapper;
    private final BusinessConfigMapper businessConfigMapper;

    @Override
    public InvoicePartyConfig getInvoicePartyConfig(String clientCode) {
        BusinessConfig businessConfig = QueryChain.of(businessConfigMapper)
                .eq(BusinessConfig::getCode, clientCode)
                .get();
        InvoicePartyConfig invoicePartyConfig = QueryChain.of(invoicePartyConfigMapper)
                .eq(InvoicePartyConfig::getCode, businessConfig.getPartyConfigCode())
                .get();
        if (ObjectUtil.isEmpty(invoicePartyConfig)) {
            throw new PermissionDeniedException("客户端校验失败");
        }
        return invoicePartyConfig;
    }

    @Override
    public InvoicePartyConfig getInvoicePartyConfigByCode(String code) {
        InvoicePartyConfig invoicePartyConfig = QueryChain.of(invoicePartyConfigMapper)
                .eq(InvoicePartyConfig::getCode, code)
                .get();
        if (ObjectUtil.isEmpty(invoicePartyConfig)) {
            throw new PermissionDeniedException("发票配置信息不存在");
        }
        return invoicePartyConfig;
    }
}
